package com.eyas.business.service.admininfo;

import com.eyas.business.config.webmvc.exception.custom.uncheck.EyasBusinessException;
import com.eyas.business.config.webmvc.interceptor.LoginInterceptor;
import com.eyas.business.model.jdbcEntity.table.AdminInfo;
import com.eyas.business.model.jdbcEntity.table.CoachInfo;
import com.eyas.business.model.jdbcEntity.table.UserAndRole;
import com.eyas.business.model.pojo.admininf.QueryPageParmDTO;
import com.eyas.business.model.pojo.admininf.QueryPageResultDTO;
import static com.eyas.core.jdbc.utils.CommonJdbcUtils.*;
import com.eyas.core.model.page.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/2 15:49
 * @Description:
 */
@Service
@Transactional
public class AdminInfoServiceImpl implements AdminInfoService {
    @Override
    public PageResult<QueryPageResultDTO> queryAdminInfo(QueryPageParmDTO queryPageParmDTO, int page, int limit) {
        StringBuffer sb = new StringBuffer("");
        List list = new ArrayList<Object>();
        sb.append(" select a.*,b.username as addusername,e.rolename from admininfo a left join admininfo b ")
                .append(" on a.addid=b.adminnid and b.validflag='1' ")
                .append(" left join (select d.adminid,c.rolename from USERANDROLE D,roleinfo c where c.roleid=d.roleid  ) e ON e.ADMINID=A.ADMINNID   ")
                .append(" where 1=1 ");
        if(StringUtils.hasText(queryPageParmDTO.getLoginname())) {
            sb.append(" and a.loginname= ? ");
            list.add(queryPageParmDTO.getLoginname());
        }
        if(StringUtils.hasText(queryPageParmDTO.getUsername())) {
            sb.append(" and a.username like ? ");
            list.add("%"+queryPageParmDTO.getUsername()+"%");
        }
        if(StringUtils.hasText(queryPageParmDTO.getValidflag())) {
            sb.append(" and a.validflag = ? ");
            list.add(queryPageParmDTO.getValidflag());
        }
        if(queryPageParmDTO.getStarttime()!=null) {
            sb.append(" and a.addtime >= ? ");
            list.add(queryPageParmDTO.getStarttime());
        }
        if(queryPageParmDTO.getEndtime()!=null) {
            sb.append(" and a.addtime <= ? ");
            list.add(queryPageParmDTO.getEndtime());
        }

        PageResult<QueryPageResultDTO> pageResult = commonJdbcUtils.queryForPage(sb.toString(),QueryPageResultDTO.class,page,limit,list);
        return pageResult;
    }

    @Override
    public void updateValidFlag(int adminid) {
        AdminInfo adminInfo = commonJdbcUtils.queryObject("select * from admininfo where adminnid=?",AdminInfo.class,adminid);
        if(adminInfo==null)
            throw new EyasBusinessException("数据缺失，请刷新页面重试");
        if(adminInfo.getValidflag().equals("1")) {
            adminInfo.setValidflag("0");
        }else {
            int count = commonJdbcUtils.queryObject("select count(1) from admininfo where loginname=? and validflag='1'",Integer.class,adminInfo.getLoginname());
            if(count>=1)
                throw new EyasBusinessException("该登录账号已存在有效的数据，无法置为有效");
            adminInfo.setValidflag("1");
        }
        commonJdbcUtils.update(adminInfo);
    }

    @Override
    public QueryPageResultDTO getAdminInfo(int adminnid) {
        QueryPageResultDTO adminInfo = commonJdbcUtils.queryObject("select a.*,b.roleid from admininfo a left join userandrole b " +
                " on a.adminnid=b.adminid where a.adminnid=?",QueryPageResultDTO.class,adminnid);
        return adminInfo;
    }

    @Override
    public void saveAdminInfo(QueryPageParmDTO adminInfo, HttpSession session) {
        com.eyas.business.model.jpa.AdminInfo LOGIN_ADMIN = (com.eyas.business.model.jpa.AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME);

        if(adminInfo.getAdminnid()!=null&&adminInfo.getAdminnid()>0) { //update
            int count = commonJdbcUtils.queryObject("select count(1) from admininfo where loginname=? and validflag='1' and adminnid<>?",
                    Integer.class,adminInfo.getLoginname(),adminInfo.getAdminnid());
            if(count>=1)
                throw new EyasBusinessException("该登录账号已存在有效的数据，");
            AdminInfo adminInfo1 = commonJdbcUtils.queryObject("select * from admininfo where adminnid=?",AdminInfo.class,adminInfo.getAdminnid());
            if(StringUtils.hasText(adminInfo.getSex()))
                adminInfo1.setSex(adminInfo.getSex());

            //排除sex和validflag、addid、addtime、password，其他的字段进行更新
            BeanUtils.copyProperties(adminInfo,adminInfo1,"sex","validflag","addid","addtime","password");
            commonJdbcUtils.update(adminInfo1);

            //处理roleid
            if(adminInfo.getRoleid()==null||adminInfo.getRoleid()==0) {
                commonJdbcUtils.delete("delete from userandrole where adminid=?",adminInfo.getAdminnid());
            }else {
                if(commonJdbcUtils.update("update userandrole set roleid=? where adminid=?",adminInfo.getRoleid(),adminInfo.getAdminnid())==0) {
                    commonJdbcUtils.insert("insert into userandrole(roleid,adminid) values(?,?)",adminInfo.getRoleid(),adminInfo.getAdminnid());
                }
            }

        }else {// insert
            int count = commonJdbcUtils.queryObject("select count(1) from admininfo where loginname=? and validflag='1'",Integer.class,adminInfo.getLoginname());
            if(count>=1)
                throw new EyasBusinessException("该登录账号已存在有效的数据，");
            AdminInfo adminInfo1 = new AdminInfo();
            //排除sex和validflag、addid、addtime、password，其他的字段进行更新
            BeanUtils.copyProperties(adminInfo,adminInfo1,"sex","validflag","addid","addtime","password");
            if(!StringUtils.hasText(adminInfo.getSex()))
                adminInfo1.setSex("1");
            else
                adminInfo1.setSex(adminInfo.getSex());
            adminInfo1.setValidflag("1");
            adminInfo1.setAddtime(new Date());
            adminInfo1.setPassword("111111");
            adminInfo1.setAddid(LOGIN_ADMIN.getAdminnid());
            commonJdbcUtils.insert(adminInfo1);
            //处理roleid
            if(adminInfo.getRoleid()!=null&&adminInfo.getRoleid()>0) {
                UserAndRole userAndRole = new UserAndRole();
                userAndRole.setAdminid(adminInfo1.getAdminnid());
                userAndRole.setRoleid(adminInfo.getRoleid());
                commonJdbcUtils.insert(userAndRole);
            }
        }
    }
    
    @Override
    public List<AdminInfo> queryAdminInfoList() {
        return commonJdbcUtils.queryList("select adminnid,username from admininfo where validflag='1'",AdminInfo.class);
    }

    @Override
    public void modifyPassword(QueryPageParmDTO pageParmDTO) {
        commonJdbcUtils.update("update admininfo set password=? where adminnid=?",pageParmDTO.getPassword(),pageParmDTO.getAdminnid());
    }
}
